import md5 from "@/Grace6/js/md5.js";
import GraceRequestConfig from "@/custom/graceRequestConfig.js";
import getWebSocketClient from "@/Grace6/js/ws";

export default {
  // 版本检查
  verson: function () {
    var currentVersion = "6.0";
    console.log(currentVersion);
    return currentVersion;
  },
  // --- 页面跳转相关 ---
  // 页面跳转
  navigate: function (url, type, success, fail, complete) {
    console.log("navigate");
    if (!type) {
      type = "navigateTo";
    }
    if (!success) {
      success = function () {};
    }
    if (!fail) {
      fail = function () {};
    }
    if (!complete) {
      complete = function () {};
    }
    switch (type) {
      case "navigateTo":
        uni.navigateTo({
          url: url,
          success: success,
          fail: fail,
          complete: complete,
        });
        break;
      case "redirectTo":
        uni.redirectTo({
          url: url,
          success: success,
          fail: fail,
          complete: complete,
        });
        break;
      case "switchTab":
        uni.switchTab({
          url: url,
          success: success,
          fail: fail,
          complete: complete,
        });
        break;
      case "reLaunch":
        uni.reLaunch({
          url: url,
          success: success,
          fail: fail,
          complete: complete,
        });
        break;
    }
  },
  // 返回
  back: function (delta) {
    if (!delta) {
      delta = 1;
    }
    uni.navigateBack({
      delta: delta,
    });
  },

  // --- 网络请求 ---
  // get
  get: function (url, data, headers, success, fail) {
    if (!fail) {
      fail = () => {
        this.msg("网络请求失败");
      };
    }
    if (!headers) {
      headers = {};
    }
    if (
      !this.isEmpty(this.getStorage("token")) &&
      this.getStorage("token") != false
    ) {
      headers["token"] = this.getStorage("token");
    }
    if (this.__before != null) {
      this.__before();
      this.__before = null;
    }
    uni.request({
      url: GraceRequestConfig.apiBaseUrl + url,
      data: data,
      method: "GET",
      dataType: "json",
      header: headers,
      success: (res) => {
        if (res.data.code == 200) {
          success(res.data);
        } else if (res.data.code == 700) {
          this.msg(res.data.message);
          this.logout();
        } else if (res.data.code == 500) {
          success(res.data);
          this.msg(res.data.message);
        } else {
          this.msg("网络错误");
        }
      },
      fail: fail,
      complete: () => {
        if (this.__after != null) {
          this.__after();
          this.__after = null;
        }
      },
    });
  },
  logout() {
    //退出登陆
    this.removeStorage("token");
    this.removeStorage("user");
    const wsClient = getWebSocketClient();
    console.log(wsClient);
    wsClient.close();
    this.navigate("/pages/login/login", "reLaunch");
  },
  // post
  post: function (url, data, contentType, headers, success, fail) {
    // uni.showLoading({ title: "请求中" });
    if (!fail) {
      fail = () => {
        this.msg("网络请求失败");
      };
    }
    if (!headers) {
      headers = {};
    }
    if (
      !this.isEmpty(this.getStorage("token")) &&
      this.getStorage("token") != false
    ) {
      headers["token"] = this.getStorage("token");
    }
    if (!contentType) {
      contentType = "json";
    }
    if (this.__before != null) {
      this.__before();
      this.__before = null;
    }
    switch (contentType) {
      case "form":
        headers["content-type"] = "application/x-www-form-urlencoded";
        break;
      case "json":
        headers["content-type"] = "application/json";
        break;
      default:
        headers["content-type"] = "application/x-www-form-urlencoded";
    }
    uni.request({
      url: GraceRequestConfig.apiBaseUrl + url,
      data: data,
      method: "POST",
      dataType: "json",
      header: headers,
      success: (res) => {
        // console.log(res);
        uni.hideLoading();
        if (res.data.code != 2000) {
          if (res.data.code == 700) {
            //退出登陆
            this.removeStorage("token");
            this.removeStorage("user");
            const wsClient = getWebSocketClient();
            console.log(wsClient);
            wsClient.close();
            this.navigate("/pages/login/login", "reLaunch");
          }
          this.msg(res.data.message);
          return false;
        } else {
          // console.log(res.data,'hhh');
          // console.log(success(),'jjj')
          success(res.data);
        }
      },
      fail: fail,
      complete: () => {
        uni.hideLoading();

        if (this.__after != null) {
          this.__after();
          this.__after = null;
        }
      },
    });
  },
  // 请求前置函数
  __before: null,
  setBefore: function (func) {
    this.__before = func;
  },
  // 请求后置函数
  __after: null,
  setAfter: function (func) {
    this.__after = func;
  },

  // --- 数据缓存 ---
  setStorage: function (key, val) {
    try {
      uni.setStorageSync(key, val);
      return true;
    } catch (e) {
      return false;
    }
  },
  getStorage: function (keyName) {
    try {
      var tmpVal = uni.getStorageSync(keyName);
      if (tmpVal == "") {
        return false;
      }
      return tmpVal;
    } catch (e) {
      return false;
    }
  },
  removeStorage: function (keyName) {
    try {
      console.log("删除了缓存" + keyName);
      uni.removeStorageSync("userInfo");
      uni.removeStorageSync(keyName);
      return true;
    } catch (e) {
      console.log(e);
      return false;
    }
  },
  clearStorage: function () {
    try {
      uni.clearStorageSync();
    } catch (e) {}
  },

  // --- 图片相关 ---
  chooseImgs: function (sets, success, fail, complete) {
    if (!sets.count) {
      sets.count = 1;
    }
    if (!sets.sizeType) {
      sets.sizeType = ["original", "compressed"];
    }
    if (!sets.sourceType) {
      sets.sourceType = ["album", "camera"];
    }
    uni.chooseImage({
      count: sets.count, //默认9
      sizeType: sets.sizeType, //可以指定是原图还是压缩图，默认二者都有
      sourceType: sets.sourceType, //从相册选择
      success: (res) => {
        success(res.tempFilePaths);
      },
      fail: (e) => {
        if (fail) {
          fail(e);
        }
      },
      complete: (e) => {
        if (complete) {
          complete(e);
        }
      },
    });
  },
  getImageInfo: function (imgUrl, success, fail, complete) {
    uni.getImageInfo({
      src: imgUrl,
      success: function (info) {
        success(info);
      },
      fail: (e) => {
        if (fail) {
          fail(e);
        }
      },
      complete: (e) => {
        if (complete) {
          complete(e);
        }
      },
    });
  },
  previewImage: function (items, currentImg) {
    uni.previewImage({
      urls: items,
      current: currentImg,
    });
  },

  // --- 系统信息 ---
  system: function () {
    try {
      var res = uni.getSystemInfoSync();
      var iPhoneXBottom = 0;
      if (!res.model) {
        res.model = "no";
      }
      res.model = res.model.replace(" ", "");
      res.model = res.model.toLowerCase();
      var res1 = res.model.indexOf("iphonex");
      if (res1 > 5) {
        res1 = -1;
      }
      var res2 = res.model.indexOf("iphone1");
      if (res2 > 5) {
        res2 = -1;
      }
      if (res1 != -1 || res2 != -1) {
        res.iPhoneXBottomHeightRpx = 60;
        res.iPhoneXBottomHeightPx = uni.upx2px(60);
      } else {
        res.iPhoneXBottomHeightRpx = 0;
        res.iPhoneXBottomHeightPx = 0;
      }
      return res;
    } catch (e) {
      return null;
    }
  },
  isEmpty: function (obj) {
    if (
      typeof obj == "undefined" ||
      obj == null ||
      obj == "" ||
      obj == undefined ||
      obj == " " ||
      obj == "undefined" ||
      obj == "null"
    ) {
      return true;
    } else {
      return false;
    }
  },
  // --- 消息弹框 ---
  msg: function (msg) {
    uni.showToast({
      title: msg,
      icon: "none",
    });
  },
  showLoading: function (title) {
    uni.showLoading({
      title: title,
      mask: true,
    });
  },

  // --- 导航条设置 ---
  setNavBar: function (sets) {
    if (sets.title) {
      uni.setNavigationBarTitle({
        title: sets.title,
      });
    }
    if (sets.color) {
      uni.setNavigationBarColor({
        frontColor: sets.color.frontColor,
        backgroundColor: sets.color.backgroundColor,
        animation: {
          duration: 400,
          timingFunc: "easeIn",
        },
      });
    }
    if (sets.loading) {
      uni.showNavigationBarLoading();
    } else {
      uni.hideNavigationBarLoading();
    }
  },

  // --- 元素选择 ---
  // 单个元素选择
  select: function (selector, callBack) {
    uni
      .createSelectorQuery()
      .select(selector)
      .boundingClientRect()
      .exec((res) => {
        callBack(res[0]);
      });
  },
  // 多个元素获取
  selectAll: function (selector, callBack) {
    uni
      .createSelectorQuery()
      .selectAll(selector)
      .boundingClientRect()
      .exec((res) => {
        callBack(res[0]);
      });
  },

  // --- 数组操作 ---
  // 数组合并
  arrayConcat: function () {
    var tmpArr = [];
    for (let i = 0; i < arguments.length; i++) {
      tmpArr = tmpArr.concat(arguments[i]);
    }
    return tmpArr;
  },
  arrayDrop: function (array, index, howmany) {
    if (!index) {
      index = 0;
    }
    if (!howmany) {
      howmany = 1;
    }
    array.splice(index, howmany);
    return array;
  },
  arrayIndexOf: function (arr, needFind) {
    var index = -1;
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == needFind) {
        index = i;
        return i;
      }
    }
    return index;
  },
  arrayDifference: function (a, b) {
    const set = new Set(b);
    return a.filter((x) => !set.has(x));
  },
  arrayShuffle: function (arr) {
    let l = arr.length;
    while (l) {
      const i = Math.floor(Math.random() * l--);
      [arr[l], arr[i]] = [arr[i], arr[l]];
    }
    return arr;
  },
  arraySum: function (arr) {
    return arr.reduce((acc, val) => acc + val, 0);
  },
  arrayAvg: function (arr) {
    return arr.reduce((acc, val) => acc + val, 0) / arr.length;
  },
  arrayEach: function (arr, fun) {
    for (let i = 0; i < arr.length; i++) {
      fun(arr[i], i);
    }
  },

  // 2数之间的随机数
  random: function (min, max) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * min + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * (max - min + 1) + min, 10);
        break;
      default:
        return 0;
    }
  },

  // UUID
  uuid: function (len) {
    var chars =
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
        "",
      );
    var uuid = [],
      i;
    if (len) {
      for (i = 0; i < len; i++) {
        uuid[i] = chars[0 | (Math.random() * chars.length)];
      }
    } else {
      var r;
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
      uuid[14] = "4";
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | (Math.random() * 16);
          uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
        }
      }
    }
    return uuid.join("");
  },

  // --- 日期时间 ---
  now: function (type, addTime) {
    var dateObj = new Date();
    var cTime = dateObj.getTime();
    if (addTime) {
      cTime += addTime;
    }
    if (!type) {
      type = "number";
    }
    if (type == "number") {
      return cTime;
    } else if (type == "str") {
      return this.toDate(cTime / 1000, "str");
    } else if (type == "array") {
      return this.toDate(cTime / 1000, "array");
    }
  },
  // 时间戳转 YY-mm-dd HH:ii:ss
  toDate: function (timeStamp, returnType) {
    timeStamp = parseInt(timeStamp);
    var date = new Date();
    if (timeStamp < 90000000000) {
      date.setTime(timeStamp * 1000);
    } else {
      date.setTime(timeStamp);
    }
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    var d = date.getDate();
    d = d < 10 ? "0" + d : d;
    var h = date.getHours();
    h = h < 10 ? "0" + h : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    if (returnType == "str") {
      return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
    }
    return [y, m, d, h, minute, second];
  },
  // 字符串转时间戳
  toTimeStamp: function (timeStamp) {
    var reg =
      /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
    var res = timeStamp.match(reg);
    if (res == null) {
      var reg2 =
        /^([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
      var res2 = timeStamp.match(reg2);
      if (res2 == null) {
        console.log("时间格式错误 E001");
        return false;
      }
      var year = parseInt(res2[3]);
      var month = parseInt(res2[1]);
      var day = parseInt(res2[2]);
      var h = parseInt(res2[4]);
      var i = parseInt(res2[5]);
      var s = parseInt(res2[6]);
    } else {
      var year = parseInt(res[1]);
      var month = parseInt(res[2]);
      var day = parseInt(res[3]);
      var h = parseInt(res[4]);
      var i = parseInt(res[5]);
      var s = parseInt(res[6]);
    }
    if (year < 1000) {
      console.log("时间格式错误");
      return false;
    }
    if (h < 0 || h > 24) {
      console.log("时间格式错误");
      return false;
    }
    if (i < 0 || i > 60) {
      console.log("时间格式错误");
      return false;
    }
    if (s < 0 || s > 60) {
      console.log("时间格式错误");
      return false;
    }
    return Date.parse(new Date(year, month - 1, day, h, i, s));
  },
  messageFormatDate: function (dateStr) {
    if (this.isEmpty(dateStr)) {
      return "";
    }
    const inputDate = new Date(dateStr);
    const now = new Date();
    const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
    const oneHour = 60 * 60 * 1000; // 一小时的毫秒数
    const oneMinute = 60 * 1000;
    const twoMinutes = 2 * oneMinute;
    const todayStart = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate(),
    );
    const weekStart = new Date(now.getTime() - 7 * oneDay);

    const diff = now - inputDate;
    if (diff < twoMinutes) {
      return "刚刚";
    } else if (inputDate >= todayStart) {
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${hours}:${minutes}`;
    } else if (inputDate >= weekStart) {
      const weekDay = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ];
      const dayIndex = inputDate.getDay();
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${weekDay[dayIndex]} ${hours}:${minutes}`;
    } else if (inputDate.getFullYear() === now.getFullYear()) {
      const month = (inputDate.getMonth() + 1).toString().padStart(2, "0");
      const day = inputDate.getDate().toString().padStart(2, "0");
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${month}-${day} ${hours}:${minutes}`;
    } else {
      const year = inputDate.getFullYear();
      const month = (inputDate.getMonth() + 1).toString().padStart(2, "0");
      const day = inputDate.getDate().toString().padStart(2, "0");
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    }
  },
  formatDate: function (dateStr) {
    if (this.isEmpty(dateStr)) {
      return "";
    }
    const inputDate = new Date(dateStr);
    const now = new Date();
    const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
    const oneHour = 60 * 60 * 1000; // 一小时的毫秒数
    const oneMinute = 60 * 1000;
    const threeMinutes = 3 * oneMinute;
    const todayStart = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate(),
    );
    const yesterdayStart = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate() - 1,
    );
    const weekAgo = new Date(now.getTime() - 7 * oneDay);

    const diff = now - inputDate;
    if (diff < threeMinutes) {
      return "刚刚";
    } else if (inputDate >= todayStart) {
      const diffHours = Math.floor(diff / oneHour);
      if (diffHours === 0) {
        const diffMinutes = Math.floor(diff / oneMinute);
        return `${diffMinutes}分钟前`;
      }
      return `${diffHours}小时前`;
    } else if (inputDate >= yesterdayStart) {
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `昨天 ${hours}:${minutes}`;
    } else if (inputDate >= weekAgo) {
      const days = Math.floor((now - inputDate) / oneDay);
      return `${days}天前`;
    } else if (inputDate.getFullYear() === now.getFullYear()) {
      const month = (inputDate.getMonth() + 1).toString().padStart(2, "0");
      const day = inputDate.getDate().toString().padStart(2, "0");
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${month}-${day}`;
      // ${hours}:${minutes}
    } else {
      const year = inputDate.getFullYear();
      const month = (inputDate.getMonth() + 1).toString().padStart(2, "0");
      const day = inputDate.getDate().toString().padStart(2, "0");
      const hours = inputDate.getHours().toString().padStart(2, "0");
      const minutes = inputDate.getMinutes().toString().padStart(2, "0");
      return `${year}-${month}-${day}`;
      //	${hours}:${minutes}
    }
  },
  // 根据时间戳计算多少分钟/小时/天之前
  fromTime: function (time) {
    if (time < 90000000000) {
      time *= 1000;
    }
    var timer = new Date().getTime() - time;
    timer = parseInt(timer / 1000);
    if (timer < 180) {
      return "刚刚";
    } else if (timer >= 180 && timer < 3600) {
      return parseInt(timer / 60) + "分钟前";
    } else if (timer >= 3600 && timer < 86400) {
      return parseInt(timer / 3600) + "小时前";
    } else if (timer >= 86400 && timer < 2592000) {
      return parseInt(timer / 86400) + "天前";
    } else {
      return this.toDate(time, "str");
    }
  },

  // 延迟操作
  delay: function (timer, func) {
    return setTimeout(func, timer);
  },
  // 间隔指定时间循环某个函数
  interval: function (timer, func) {
    return setInterval(func, timer);
  },

  // 对象操作
  assign: function (obj, key, val) {
    obj[key] = val;
  },
  removeByKey: function (obj, key) {
    delete obj[key];
  },
  each: function (obj, func) {
    for (let k in obj) {
      func(k, obj[k]);
    }
  },
  isEmptyObj: function (obj) {
    return JSON.stringify(obj) === "{}";
  },

  // 获取ref ( 循环获取，直到 组件创建完成并获取成功 )
  getRefs: function (ref, _this, count, fun) {
    if (count >= 50) {
      fun(_this.$refs[ref]);
      return false;
    }
    var refReturn = _this.$refs[ref];
    if (refReturn) {
      fun(refReturn);
    } else {
      count++;
      setTimeout(() => {
        this.getRefs(ref, _this, count, fun);
      }, 100);
    }
  },

  // md5
  md5: function (str) {
    return md5.md5(str);
  },
  confirm(msg, confirmText, cancelText, success, fail) {
    uni.showModal({
      title: "提示",
      confirmText: this.isEmpty(confirmText) ? "确定" : confirmText,
      cancelText: this.isEmpty(cancelText) ? "取消" : cancelText,
      content: msg,
      success: function (res) {
        if (res.confirm) {
          success();
        } else if (res.cancel) {
          try {
            fail();
          } catch (e) {}
        }
      },
    });
  },
  checkLogin: function () {
    let token = this.getStorage("token");
    console.log("token为" + token);
    if (this.isEmpty(token)) {
      console.log("未登录");
      uni.showLoading({ title: "模拟登录中" });
      setTimeout(() => {
        this.post(
          "/checkCode",
          { phoneNumber: 123, code: 111 },
          "",
          "",
          (res) => {
            if (res.code == 200) {
              uni.hideLoading();
              //保存token
              this.setStorage("token", res.data.token);
              this.setStorage("user", res.data);
              this.msg("欢迎回来" + res.data.authorName);
              this.navigate("/pages/switchPages/index", "reLaunch");
            }
          },
        );
      }, 1000);

      //
      // this.confirm('未登录，即将跳转至登录页？', '去登陆', '返回主页', () => {
      // 	this.navigate('/pages/login/login', 'reLaunch')
      // }, () => {
      // 	this.navigate('/pages/switchPages/index', 'reLaunch')
      // })
    } else {
      console.log("已登录");
      return true;
    }
  },
  goCard(authorId) {
    let user = this.getStorage("user");
    console.log("goCard=================================");
    console.log(authorId == user.authorId);
    if (authorId == user.authorId) {
      this.navigate("/pages/switchPages/mine", "switchTab");
    } else {
      console.log("跳转对方名片" + authorId);
      this.navigate("/pages/card/card?authorId=" + authorId);
    }
  },
  goChat(author) {
    this.navigate(
      "/pages/chat/chat?authorId=" +
        author.authorId +
        "&authorName=" +
        author.authorName,
    );
  },
  getTotalUnReadCount() {
    const wsClient = getWebSocketClient();
    wsClient.sendMessage(
      JSON.stringify({
        handlerType: "9",
        authorId: this.getStorage("user").authorId,
      }),
    );
  },
  readAuthorMessage(authorId) {
    const wsClient = getWebSocketClient();
    wsClient.sendMessage(
      JSON.stringify({
        handlerType: "7",
        authorId: authorId,
        loginId: this.getStorage("user").authorId,
      }),
    );
  },
  //设置角标
  setMessageTabBarBadge(totalUnReadCount) {
    if (totalUnReadCount > 0) {
      uni.setTabBarBadge({
        index: 3,
        text: totalUnReadCount.toString(),
      });
    } else {
      uni.removeTabBarBadge({
        index: 3,
      });
    }
    return totalUnReadCount;
  },
  generateRandomNumber: function (minDigits, maxDigits) {
    // 计算最小数和最大数对应的数值
    const min = Math.pow(10, minDigits - 1);
    const max = Math.pow(10, maxDigits) - 1;
    // 生成并返回指定范围内的随机数
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },
};
